﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data;

namespace QuanLyShopCD.BL
{
    class cl_Danhmuc
    {
        AcessData con;
        Form frm;
        //String SQL;
        DataTable tb;

        public cl_Danhmuc(frm_QLDanhMuc2 f)
        {
            con = new AcessData();
            frm = f;          
        }
        public void Refesh()
        {
            ((DataGridView)frm.Controls["dataGridview1"]).DataSource = tb;       
            ((TextBox)frm.Controls["txt_MaDM1"]).DataBindings.Clear();
            ((TextBox)frm.Controls["txt_TenDM1"]).DataBindings.Clear();
    
            ((TextBox)frm.Controls["txt_MaDM1"]).DataBindings.Add("Text", tb, "MaDM");
            ((TextBox)frm.Controls["txt_TenDM1"]).DataBindings.Add("Text", tb, "TenDM");

        }

        public void Hienthi(string MaDM)
        {
            con.Open();
            SqlParameter []pr = new SqlParameter[1];
            if (MaDM != null)
                pr[0] = new SqlParameter("@MaDM", MaDM);
            else
                pr[0] = new SqlParameter("@MaDM", DBNull.Value);
            tb = con.Execute_ps("PS_LAY_DM", pr);         
            Refesh();
            
        }

        public void them()
        {
            con.Open();
            string str_MaDM, str_TenDM;
            str_MaDM = ((TextBox)frm.Controls["txt_madm1"]).Text;
            str_TenDM = ((TextBox)frm.Controls["txt_tendm1"]).Text;
            SqlParameter[] pr = { new SqlParameter("@MaDM", str_MaDM), new SqlParameter("@TenDM", str_TenDM) };
            tb = con.Execute_ps("PS_DANHMUC", pr);
            Refesh();
        }

        public void sua()
        {
            con.Open();
            string str_MaDM, str_TenDM;
            str_MaDM = ((TextBox)frm.Controls["txt_madm1"]).Text;
            str_TenDM = ((TextBox)frm.Controls["txt_tendm1"]).Text;
            SqlParameter[] pr = { new SqlParameter("@MaDM",str_MaDM),new SqlParameter("@TenDM",str_TenDM)};

            tb = con.Execute_ps("PS_SUADM", pr);
            ((DataGridView)frm.Controls["dataGridview1"]).DataSource = tb;
            Refesh();
        }

        public void xoa(string MaDM)
        {
            con.Open();
            SqlParameter[] pr = new SqlParameter[1];
            pr[0] = new SqlParameter("@MaDM",MaDM);
            tb = con.Execute_ps("PS_XOADM", pr);
            ((DataGridView)frm.Controls["dataGridView1"]).DataSource = tb;
            Refesh();
        }
    }
}
